#-------------------------------------
# Thuong
#-------------------------------------
class Admin::BookController < ApplicationController
  # specific layout
  layout 'admin/application'

  def index
    @books = Book.paginate(:page => params[:page], :per_page => 10)
  end
   
  def view
      @book = Book.find(params[:id])
   end
   
   def new
      @book = Book.new
      @categories = BookCategory.find(:all)
   end
   
    def create
      @book = Book.new(
        :title => params[:book][:title],
        :author => params[:book][:author],
        :publisher => params[:book][:publisher],
        :price => params[:book][:price],
        :quantity => params[:book][:quantity],
        :book_category_id => params[:book][:book_category_id],
        :free_quantity => params[:book][:free_quantity],
        :description => params[:book][:description],
        :image_url => 'books/' + params[:book][:image_url].original_filename
      )
      if @book.save
        uploaded_file = params[:book][:image_url]
        File.open(Rails.root.join('app/assets/images', 'books', uploaded_file.original_filename), 'wb') do |file|
        file.write(uploaded_file.read)
      end
        redirect_to :action => 'view', :id => @book.id
      else
        @categories = BookCategory.find(:all)
        render :action => 'new'
      end
   end
   
   def edit
      @book = Book.find(params[:id])
      @categories = BookCategory.find(:all)
   end
   
   def update
      @book = Book.find(params[:id])
      
      if @book.update_attributes(
          :title => params[:book][:title],
          :author => params[:book][:author],
          :publisher => params[:book][:publisher],
          :price => params[:book][:price],
          :quantity => params[:book][:quantity],
          :book_category_id => params[:book][:book_category_id],
          :free_quantity => params[:book][:free_quantity],
          :description => params[:book][:description]
         )
         if params[:book][:image_url] != nil
            File.delete(Rails.root.join("app/assets/images/") + @book.image_url)
            uploaded_file = params[:book][:image_url]
            File.open(Rails.root.join('app/assets/images', 'books', uploaded_file.original_filename), 'wb') do |file|
              file.write(uploaded_file.read)
            end
            @book.update_attributes(:image_url => 'books/' + params[:book][:image_url].original_filename)
         end
         redirect_to :action => 'view', :id => @book.id
      else
         @categories = BookCategory.find(:all)
         render :action => 'edit'
      end
   end
   
   def delete
      Book.find(params[:id]).destroy
      redirect_to :action => 'index'
   end

   def show_categories
      @categories = BookCategory.find(params[:id])
   end
   
   def search
     if params[:search]
       cate = params[:cate]
       keyword = params[:keyword]
       
       case cate.to_i
         when 0 then  # id
           @results_list = Book.where('id = ?', keyword)
         when 1 then  # title
           @results_list = Book.where('title LIKE ?', "%#{keyword}%")
         when 2 then  # author
           @results_list = Book.where('author LIKE ?', "%#{keyword}%")
         when 3 then  # category
           @results_list = Book.where(book_category_id: BookCategory.where("category_name LIKE ?", "%#{@keyword}%"))
         when 4 then  # publisher
           @results_list = Book.where('publisher LIKE ?', "%#{keyword}%")
         when 5 then  # price
           @results_list = Book.where('price = ?', keyword)
         when 6 then  # quantity
           @results_list = Book.where('quantity = ?', keyword)
         when 7 then  # free quantity
           @results_list = Book.where('free_quantity = ?', keyword)
       end # end case
       
       @results_list = @results_list.paginate(:page => params[:page], :per_page => 10)
     end # end if
   end
end